﻿using Dyao.Book.Contract;

namespace Dyao.Book.Application.Abstractions
{
    public static class TaskCenterParametersExtensions
    {
        public static object GetValue(this ITaskCenterParameters parameters, params string[] keys)
        {
            object v = null;
            foreach (var key in keys)
            {
                if (parameters.TryGetValue(key, out v)) break;
            }

            return v;
        }

        public static T GetValue<T>(this ITaskCenterParameters parameters, params string[] keys)
        {
            var value = default(T);

            foreach (var key in keys)
            {
                if (parameters.TryGetValue<T>(key, out value)) break;
            }

            return value;
        }

        /// <summary>
        /// 获取参数名为“RequestFilter”的参数值，反序列为 <typeparamref name="T"/> 查询条件 model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T GetFilterModel<T>(this ITaskCenterParameters parameters)
        {
            return parameters.GetValue<T>("requestFilter", "RequestFilter", "requestParams", "RequestParams","filter","Filter");
        }

        /// <summary>
        /// 获取参数名为“Params”的参数值
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string GetParamsValue(this ITaskCenterParameters parameters)
        {
            return parameters.GetParamsValue<string>();
        }

        /// <summary>
        /// 获取参数名为“Params”的参数值，反序列为 <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T GetParamsValue<T>(this ITaskCenterParameters parameters)
        {
            return parameters.GetValue<T>("params", "Params");
        }
    }
}
